package pageObject;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import utils.ReportWriter;

import java.util.List;

abstract public class BasePage {

    protected WebDriver driver;
    protected static final By loginButton = By.name("signin");
    protected static final By loginInput = By.name("login");
    protected static final By passwordInput = By.name("password");
    protected static final By loginLinkDivClassName = By.className("auth-f-submit");
    protected static final By registerButtonXpath = By.xpath("//div/div/div/form/div[5]/a");
    protected static final By allMenuClassName = By.className("m-main-t-i");
    protected static final By searchField = By.className("header-search-input-text");
    protected static final By searchButton = By.className("btn-link-i");
    protected static final By searchParameterXpath = By.xpath("//*[text()[contains(.,'A1475')]]");
    protected static final String expectedSearchResult = "http://rozetka.com.ua/apple_ipad_air_wi_fi_cellular_space_gray_16gb/p337930/";
    protected static final By goToCheckoutButtonId = By.id("popup-checkout");
    protected static final By openCartButtonName = By.name("open_cart");
    protected static final By buyButtonClassName = By.className("ibutton-css-green");

    protected BasePage(WebDriver driver) {
        this.driver = driver;
    }

    public HomePage login(String email, String password) {
        driver.findElement(loginButton).click();
        driver.findElement(loginInput).clear();
        driver.findElement(loginInput).sendKeys(email);
        driver.findElement(passwordInput).clear();
        driver.findElement(passwordInput).sendKeys(password);
        driver.findElement(loginLinkDivClassName).findElement(By.tagName("button")).click();
        return new HomePage(driver);
    }

    public boolean checkLogIn(String name) {
        boolean success = false;
        try {
            Thread.sleep(2000);
            if(driver.findElement(By.xpath("//a[.='" + name + "']")).isDisplayed()) {
                ReportWriter.info("User " + name + " is logged in!");
                success = true;
            }
        } catch (NoSuchElementException ex) {
            ReportWriter.info("User " + name + " is NOT logged in!");
            success = false;
        } catch (Exception ex) {
            ReportWriter.info("Caught exception while sleep");
        } finally {
            return success;
        }
    }

    public RegistrationPage openRegistrationPage() {
        driver.findElement(loginButton).click();
        driver.findElement(registerButtonXpath).click();
        return new RegistrationPage(driver);
    }

    public boolean verifyMenuItems() {
        ReportWriter.info("Menu check is initialized...");
        List<WebElement> allMenu;
        for(int i = 0; i < driver.findElements(allMenuClassName).size(); i++) {
            allMenu = driver.findElements(allMenuClassName);
            WebElement button = allMenu.get(i);
            String buttonHref = button.findElement(By.tagName("a")).getAttribute("href");
            ReportWriter.info("Current Button Menu href is " + buttonHref);
            button.findElement(By.tagName("a")).click();
            String buttonUrl = driver.getCurrentUrl();
            if(!buttonHref.equalsIgnoreCase(buttonUrl)) {
                ReportWriter.info("FAIL! Button Menu href is " +buttonHref+ ". And Button Menu url is "+buttonUrl+".");
                return false;
            }
        }
        return true;
    }

    public void searchProduct (String product) {
        driver.findElement(searchField).sendKeys(product);
        driver.findElement(searchButton).click();
    }

    public boolean verifySearch(String product) {
        driver.findElement(searchParameterXpath).click();
        ReportWriter.info("Product "+product+" was found!");
        if(!expectedSearchResult.equalsIgnoreCase(driver.getCurrentUrl())) {
            ReportWriter.info("FAIL! Url of the searched product " + product + " is not equal to expected value");
            return false;
        }
        else
            return true;
    }

    public PurchasePage goBuy() {
        driver.findElement(buyButtonClassName).click();
        if(!(driver.findElement(goToCheckoutButtonId).isDisplayed()))
            driver.findElement(openCartButtonName).click();
        driver.findElement(goToCheckoutButtonId).click();
        return new PurchasePage(driver);
    }
}




